home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / DLog Manager Sample / DLOGManager Sample / Source Code / CGestisciElOrd_v03.cp < prev    next >
Encoding:
Text File  |  1996-10-02  |  25.2 KB  |  1,006 lines

  1. /***
  2.  *    GestisciElOrd_v03.c
  3.  *
  4.  *    Standard File Pattern.
  5.  *    © 1994-96 Francesco Cadili.
  6.  *
  7.  *    16/09/96        1st version (Francesco Cadili),
  8.  *                    use the standard file pattern.
  9.  *
  10.  * File: "GestisciElOrd_v01.c"
  11.  *
  12.  *    Visualizza un ElOrd di dati.
  13.  *    Note sulla versione:
  14.  *        1)    Bisogna gestire lo scroll mentre il mouse seleziona del testo fuori dal
  15.  *            campo TEedit.
  16.  *        2) Bisogna gestire il doppio click.
  17.  *        3)    per ora l'array viene allocato dalla chiamata a AggiustaScroll.
  18.  *              (una volta allocato non viene più deallocato)
  19.  *        4) per ora permette al più 99 titoli
  20.  *        5)    i contenuti degli array della maschera e del valore variano da 0..numMax - 1
  21.  *            (quando vengono visualizzati vengono incrementati di 1)
  22.  *        6) Cambia la gestione del campo (quando clicco cancello/inserisco il prossimo)
  23.  *        7) Richiede "\r" alla fine per funzionare correttamente.
  24.  *
  25.  ***/
  26.  
  27. /*****    Include standard        *****/
  28. /*        #include <MacHeaders>    */
  29.  
  30. /*****    Include locali            *****/
  31. #include <stdio.h>
  32. #include <Fonts.h>
  33. #include <Controls.h>
  34. #include <Menus.h>
  35. #include <QuickDraw.h>
  36. #include "CGestisciElOrd_v03.h"
  37. #include "stringUtility_v02.h"
  38. #include "Error.h"                    // usato per gestire gli errori
  39.  
  40. /*****    Define macro            *****/
  41.  
  42. /*****    Define valori            *****/
  43.  
  44. #define noValue -1
  45. #define kStdFont systemFont
  46. #define kStdHeight 12
  47. #define noTitle        -1
  48. #define    ours(w)        (w == theDialog)
  49. #define kmaxNumData    10
  50. #define kErrOutMemory 1
  51. #define kErrRisorse 2
  52. #pragma segment CGestisciElOrd
  53.  
  54. /*****    Typedef globali            *****/
  55.  
  56. /*****    Funzioni esterne        *****/
  57.  
  58. /*****    Variabili esterne        *****/
  59.  
  60. /*****    Variabili globali        *****/
  61.             
  62. /*****    Statiche globali        *****/
  63.  
  64. /*****    Function prototyping    *****/
  65.  
  66. /*** constructor
  67.  *
  68.  ****/
  69. CdataDescRec::CdataDescRec(void)
  70. {
  71.     TEH = NULL;
  72. }
  73.         
  74. /*** destructor
  75.  *
  76.  ****/
  77. CdataDescRec::~CdataDescRec(void)
  78. {
  79.     if (TEH != NULL)
  80.         delete TEH;
  81. }
  82.  
  83. /*** costructor
  84.  *
  85.  ***/
  86. CGestisciElOrd::CGestisciElOrd(void)
  87. {
  88.     theDialog = NULL;    
  89.     numerazione = NULL;
  90.     editLine = NULL;
  91.     num = NULL;
  92.     descField = NULL;
  93. }
  94.  
  95. /*****    Function prototyping    *****/
  96.  
  97. /*** costructor
  98.  *
  99.  ***/
  100. CGestisciElOrd::~CGestisciElOrd(void)
  101. {
  102.     Dispose();
  103. }
  104.  
  105. /*** result = Alloca(theDialog, TEitem, &backGround, nField, descField, fontNum, sizeNum, canModify)
  106.  *
  107.  *    Alloca la struttura dati "gestElOrdRec" nello Heap.
  108.  *
  109.  *    Par INPUT:    'theDialog' il puntatore al dialog,
  110.  *                'TEitem' il numero di item del campo text edit,
  111.  *                'CTRLitem' il numero di item del campo di scroll,
  112.  *                '*backGround' lo sfondo del campo text edit,
  113.  *                'textFont' la fonte del campo text edit,
  114.  *                'textHeight' l'altezza del campo text edit.
  115.  *                'maxNumData' il numero massimo di campi ammassi.
  116.  *    Val OUTPUT:    il puntatore alla struttura dati se tutto ok, NULL altrimenti.
  117.  *
  118.  **************************************************************************************/
  119. Boolean CGestisciElOrd::IGestisciElOrd(DialogPtr theDialog, short TEitem,
  120.                                           RGBColor *backGround, short fontNum, 
  121.                                         short fontSize, short nField, 
  122.                                         CdataDescRec ***descField, Boolean canModify, 
  123.                                         Boolean hasScroll, Boolean hasNumberField)
  124. {
  125.     short result = true;
  126.  
  127.     this->theDialog = theDialog;
  128.     this->TEitem = TEitem;
  129.     this->backGround = *backGround;
  130.     maxNumData = kmaxNumData;    // massimo 20elemnti
  131.     this->hasNumberField = hasNumberField;
  132.     this->nField = nField;
  133.     this->descField = descField;
  134.     this->canModify = canModify;
  135.     if (InitData(fontNum, fontSize, hasScroll))
  136.     {                // errore nell'inizializzazione dei campi
  137.         Dispose();
  138.         SetErrorData(kErrOutMemory, 0, "Alloca", "inizializzazione della struttura dati", "");
  139.         return(true);
  140.     }
  141.     else
  142.         return(false);
  143. }
  144.  
  145. /*** Dispose(&elOrdHdl)
  146.  *
  147.  *    Dealloca la struttura dati "dataHdl".
  148.  *    Nota:    da chiamare prima della deallocazione del
  149.  *            dialog (**elOrdHdl)->theDialog.
  150.  *
  151.  *    Par INPUT:    '*elOrdHdl' l' Handle del dialog.
  152.  *    Val OUTPUT:    'elOrdHdl' è posto a NULL.
  153.  ****/
  154. void CGestisciElOrd::Dispose(void)
  155. {
  156.     CdataDescRec    **descPtr = NULL;
  157.     short i;
  158.         
  159.     HLock((Handle)descField);
  160.     descPtr = *descField;
  161.     for (i = 0; i < nField; i++, descPtr++)
  162.         if (descPtr != NULL)
  163.                 delete *descPtr;
  164.             else
  165.                 break;
  166.     HUnlock((Handle)descField);
  167.         
  168.     if (num != NULL)
  169.         DisposHandle((Handle)num);
  170.     if (editLine != NULL)
  171.         delete editLine;
  172. }
  173.  
  174. /*** "InitData(textFont, textHeight)"
  175. *
  176. *    Inizializza i dati relativi al DLOG.
  177. *    (Presuppone che i primi dati della struttura globale
  178. *    siano presenti).
  179. *
  180. *    Par INPUT:    'textFont' la fonte del campo text edit,
  181. *                'textHeight' l'altezza del campo text edit.
  182. *    Val OUTPUT:    TRUE se tutto ok, FALSE altrimenti.
  183. *
  184. *************************************************************************/
  185. int CGestisciElOrd::InitData(short fontNum, short fontSize, Boolean hasScroll)
  186. {
  187.     const short     fieldNumWidth = 20;
  188.     CdataDescRec    **descPtr = NULL;
  189.     GrafPtr            port;
  190.     short            lineHeight = 0;
  191.     short            fontAscent = 0;
  192.     short            right, i, j;
  193.     short             iType;                    // usati per settare i dati del DLOG
  194.     Rect             iRect;
  195.     Handle             iHandle;
  196.     TEStyleRec        **theStyle;
  197.  
  198.     HLock((Handle)descField);
  199.     descPtr = *descField;
  200.     GetDItem(theDialog, TEitem, &iType, &iHandle, &iRect);
  201.     right = iRect.right;
  202.     if (hasNumberField)
  203.     {
  204.         iRect.right = iRect.left + fieldNumWidth;            // il campo numerazione è largo 'fieldNumWidth' pixel.
  205.         numerazione = new CGestisci_TE;
  206.         if (numerazione == NULL || 
  207.             numerazione->IGestisci_TE(theDialog, 0, iRect, &backGround, 
  208.                                     fontNum, fontSize, false, false, true, false, 0, false, false, false))
  209.         {
  210.             SetErrorData(kErrOutMemory, 0, "CGestisciElOrd::InitData", "CGestisciElOrd.cp", "errore durante la numerazione");
  211.             HUnlock((Handle)descField);
  212.             return(true);
  213.         }
  214.         numerazione->overBox = false;
  215.         iRect.left = iRect.left + fieldNumWidth + 1;        // lascio un pixel tra i due campi.
  216.         theStyle = GetStylHandle(numerazione->edit);
  217.         lineHeight = (**(**theStyle).styleTab).stHeight;
  218.         fontAscent = (**(**theStyle).styleTab).stAscent;
  219.         iRect.left = iRect.right + 1;
  220.     }
  221.     for (i = 1; i < nField; i++, descPtr++)
  222.     {
  223.         iRect.right = iRect.left + (**descPtr).dim;            // il campo numerazione è largo 'fieldNumWidth' pixel.
  224.         (**descPtr).TEH = new CGestisci_TE;
  225.         if ((**descPtr).TEH == NULL || 
  226.             (**descPtr).TEH->IGestisci_TE(theDialog, 0, iRect, &backGround, (**descPtr).fontNum, 
  227.                                         (**descPtr).fontSize, false, false, true, (**descPtr).canModify, 0, false, false, false))
  228.         {
  229.             SetErrorData(kErrOutMemory, 0, "CGestisciElOrd::InitData", 
  230.             "CGestisciElOrd_v03.cp", "inizializzazione della struttura dati");
  231.             HUnlock((Handle)descField);
  232.             return(true);
  233.         }
  234.         (**descPtr).TEH->overBox = false;
  235.         iRect.left = iRect.right + 1;
  236.     }
  237.     iRect.right = right;
  238.     (**descPtr).TEH  = new CGestisci_TE;
  239.     if ((**descPtr).TEH  == NULL ||
  240.         (**descPtr).TEH->IGestisci_TE(    theDialog, 0, iRect, &backGround, fontNum, 
  241.                                         fontSize, hasScroll, false, !hasScroll, (**descPtr).canModify, 0))
  242.     {
  243.         SetErrorData(kErrOutMemory, 0, "CGestisciElOrd::InitData", 
  244.         "CGestisciElOrd_v03.cp", "inizializzazione della struttura dati");
  245.         HUnlock((Handle)descField);
  246.         return(true);
  247.     }
  248.     (**descPtr).TEH->overBox = false;
  249.     scrollTEH = (**descPtr).TEH;
  250.     descPtr = *descField;
  251.     GetPort(&port);
  252.     SetPort(theDialog);
  253.     for (i = 0; i < nField; i++, descPtr++)
  254.     {
  255.         if (lineHeight != 0)
  256.         {
  257.             short diffHeight;
  258.     
  259.             theStyle = GetStylHandle((**descPtr).TEH->edit);
  260.             diffHeight = (**(**theStyle).styleTab).stHeight - lineHeight;
  261.             if (diffHeight > 0)
  262.             {
  263.                 lineHeight = (**(**theStyle).styleTab).stHeight;
  264.                 fontAscent = (**(**theStyle).styleTab).stAscent;
  265.                 if (hasNumberField)
  266.                 {
  267.                     theStyle = GetStylHandle(numerazione->edit);
  268.                     (**(**theStyle).styleTab).stHeight = lineHeight;
  269.                     (**(**theStyle).styleTab).stAscent = fontAscent;
  270.                 }
  271.                 descPtr = *descField;
  272.                 for (j = 0; j < i; j++, descPtr++)
  273.                 {
  274.                     theStyle = GetStylHandle((**descPtr).TEH->edit);
  275.                     (**(**theStyle).styleTab).stHeight = lineHeight;
  276.                     (**(**theStyle).styleTab).stAscent = fontAscent;
  277.                 }
  278.             }
  279.             else if (diffHeight < 0)
  280.             {
  281.                 (**(**theStyle).styleTab).stHeight = lineHeight;
  282.                 (**(**theStyle).styleTab).stAscent = fontAscent;
  283.             }
  284.         }
  285.         else
  286.         {
  287.             theStyle = GetStylHandle((**descPtr).TEH->edit);
  288.             lineHeight = (**(**theStyle).styleTab).stHeight;
  289.             fontAscent = (**(**theStyle).styleTab).stAscent;
  290.         }
  291.     }
  292.     descPtr = *descField;
  293.     linee = ((**(**descPtr).TEH->edit).viewRect.bottom -
  294.             (**(**descPtr).TEH->edit).viewRect.top)/(**(**descPtr).TEH->edit).lineHeight;
  295.     titoloScelto = noTitle;
  296.     this->fieldNumWidth = fieldNumWidth;
  297.     num = NULL;            // il campo che contiene la numerazione.
  298.     HUnlock((Handle)descField);
  299.     TextFont(kStdFont);
  300.     TextSize(kStdHeight);
  301.     SetPort(port);
  302.     return(false);
  303. }
  304.  
  305. /*** inizializzaNumerazione()
  306. *
  307. *    inizializza la numerazione.
  308. *    per ora presuppone che il campo text edit sia vuoto
  309. *    (la prima parte dell'array costituisce la mascera per la
  310. *    seconda parte) !!!
  311. *
  312. **************************************************************/
  313. Boolean CGestisciElOrd::inizializzaNumerazione(void)
  314. {
  315.     if (hasNumberField)
  316.     {
  317.         if (num != NULL)
  318.             return(false);
  319.     
  320.         num = (short **)NewHandle(sizeof(short) * maxNumData * 2);
  321.         if (num == NULL)
  322.         {
  323.             num = 0;
  324.             return(false);
  325.         }
  326.         else
  327.             azzeraNumerazione();
  328.         return(false);
  329.     }
  330.     else
  331.         return(true);
  332. }
  333.  
  334. /*** azzeraNumerazione()
  335. *
  336. *    Azzera l'array della numerazione.
  337. *
  338. ********************************************/
  339. void CGestisciElOrd::azzeraNumerazione(void)
  340. {
  341.     if (hasNumberField)
  342.     {
  343.         short i;
  344.         short *numVal;
  345.         short *numMasc;
  346.         
  347.         if (num == NULL)
  348.             return;
  349.         
  350.         HLock((Handle)num);
  351.         numVal = *num;
  352.         numMasc = &numVal[maxNumData];
  353.         for (i = 0; i < maxNumData; i++)
  354.             numVal[i] = numMasc[i] = noValue;                // non ho inserito alcun dato
  355.         HUnlock((Handle)num);
  356.         actNumSet = 0;
  357.     }
  358. }
  359.  
  360. /*** sostituisciNumerazione()
  361. *
  362. *    Presuppone che il campo textEdit sia vuoto.
  363. *
  364. **************************************************/
  365. void CGestisciElOrd::sostituisciNumerazione(void)
  366. {
  367.     if (hasNumberField)
  368.     {
  369.         short         i;
  370.         short         maxNum = (**(***descField).TEH->edit).nLines;
  371.         short         *numVal;
  372.         short         *numMasc;
  373.         Cstring_P    numero;
  374.         
  375.         if (num == NULL || numero.Istring_P())
  376.             return;
  377.         
  378.         HLock((Handle)num);
  379.         numVal = *num;
  380.         numMasc = &numVal[maxNumData];
  381.         numerazione->sostituisciTesto(&numero);    //    cancella tutto
  382.         numero.Ctransfer("  \r");
  383.         for (i = 0; i < maxNum; i++)
  384.             numerazione->aggiungiTesto(&numero);    // per ora suppongo che la lunghezza sia 2
  385.         HUnlock((Handle)num);
  386.     }
  387. }
  388.  
  389. /*** scambiaValori(numSet)
  390. *
  391. *    Presuppone che il campo textEdit sia vuoto.
  392. *
  393. **************************************************/
  394. void CGestisciElOrd::scambiaValori(void)
  395. {
  396.     if (hasNumberField)
  397.     {
  398.         short         *numVal;
  399.         short         *numMasc;
  400.         short         numSet = actNumSet;
  401.         RGBColor    BackColor;
  402.         RGBColor    BackGround = backGround;
  403.     
  404.         if (num == NULL)
  405.             return;
  406.     
  407.         HLock((Handle)num);
  408.         numVal = *num;
  409.         numMasc = &numVal[maxNumData];
  410.         GetBackColor(&BackColor);
  411.         RGBBackColor(&BackGround);
  412.         if (numVal[titoloScelto] != noValue)
  413.         {
  414.             short scambia = numVal[titoloScelto] + 1;
  415.     
  416.             numVal[numMasc[scambia - 1]] = -1;
  417.             Riscrivi(numVal, numMasc[scambia - 1]);
  418.             numMasc[scambia - 1] = -1;
  419.     
  420.             for (; scambia < numSet; scambia++)
  421.             {
  422.                 numVal[numMasc[scambia]] -= 1;
  423.                 numMasc[scambia - 1] = numMasc[scambia];
  424.                 Riscrivi(numVal, numMasc[scambia]);
  425.             }
  426.             numSet -= 1;                    // numSet > 0 perchè ho già inserito un dato
  427.             numMasc[numSet] = -1;            // numSet >= 0 perchè ho già inserito un dato
  428.         }
  429.         else
  430.         {                    // ho selezionato un campo vuoto
  431.             numVal[titoloScelto] = numSet;
  432.             numMasc[numSet] = titoloScelto;
  433.             Riscrivi(numVal, titoloScelto);
  434.             numSet += 1;
  435.         }
  436.         RGBBackColor(&BackColor);
  437.         HUnlock((Handle)num);
  438.         actNumSet = numSet;
  439.     }
  440. }
  441.  
  442. /*** scambiaValArray(array, pos1, pos2);
  443. *
  444. *    Scambia i valori contenuti nell'array nelle posizioni "pos1" e "pos2".
  445. *
  446. *****************************************************************************/
  447. void CGestisciElOrd::scambiaValArray(short *array, short pos1, short pos2)
  448. {
  449.     if (hasNumberField)
  450.     {
  451.         short scambio = array[pos1];
  452.         
  453.         array[pos1] = array[pos2];
  454.         array[pos2] = scambio;
  455.     }
  456. }
  457.  
  458. /*** Riscrivi(num, pos)
  459. *
  460. *    Riscrive i dati nella posizione "num".
  461. *
  462. *******************************************************/
  463. void CGestisciElOrd::Riscrivi(short *num, short pos)
  464. {
  465.     if (hasNumberField)
  466.     {
  467.         char numero[10];
  468.         short carPrecedente = (**numerazione->edit).lineStarts[pos];
  469.     
  470.         if (num[pos] >= 0)
  471.             sprintf(numero, "%2i", num[pos] + 1);                            // per ora suppongo che la larghezza massima sia di due (vedi manuale del THINK per correzione)
  472.         else
  473.             sprintf(numero, "  ");                                            // per ora suppongo che la larghezza massima sia di due (vedi manuale del THINK per correzione)
  474.     
  475.         TESetSelect(carPrecedente, carPrecedente + 2, numerazione->edit);    // ha supposto che la lunghezza massima sia 3
  476.         TEDelete(numerazione->edit);
  477.         TEInsert(numero, 2, numerazione->edit);
  478.     }
  479. }
  480.  
  481. /*** insertEl(num)
  482. *
  483. *    Riscrive i dati nella posizione "num".
  484. *
  485. *******************************************************/
  486. void CGestisciElOrd::insertEl(short pos)
  487. {
  488.     if (hasNumberField)
  489.     {
  490.         char numero[10];
  491.         short carPrecedente = (**numerazione->edit).lineStarts[pos];
  492.     
  493.         TESetSelect(carPrecedente, carPrecedente, numerazione->edit);    // ha supposto che la lunghezza massima sia 3
  494.         sprintf(numero, "  \r");                                        // per ora suppongo che la larghezza massima sia di due (vedi manuale del THINK per correzione)
  495.         TEInsert(numero, 3, numerazione->edit);
  496.     }
  497. }
  498.  
  499. /*** CancellaEl(num)
  500. *
  501. *    Riscrive i dati nella posizione "num".
  502. *
  503. *******************************************************/
  504. void CGestisciElOrd::CancellaEl(short pos)
  505. {
  506.     if (hasNumberField)
  507.     {
  508.         short carPrecedente = (**numerazione->edit).lineStarts[pos];
  509.     
  510.         TESetSelect(carPrecedente, carPrecedente, numerazione->edit);    // ha supposto che la lunghezza massima sia 3
  511.         TEDelete(numerazione->edit);
  512.     }
  513. }
  514.  
  515.  
  516. /*** "showActSel()"
  517.  *
  518.  *    mostra la selezione attuale
  519.  *
  520.  ***/
  521. void CGestisciElOrd::showActSel(void)
  522. {
  523.     TEHandle         TEH = (***descField).TEH->edit;
  524.     RGBColor         BackColor;
  525.     GrafPtr            port;
  526.     short             carPrecedente = 0;
  527.     CdataDescRec    **descPtr = NULL;
  528.     short i;
  529.  
  530.     GetPort(&port);
  531.     SetPort((GrafPtr)theDialog);
  532.     GetBackColor(&BackColor);
  533.     RGBBackColor(&backGround);
  534.     HLock((Handle)descField);
  535.     descPtr = *descField;
  536.     for (i = 0; i < nField; i++, descPtr++)
  537.     {
  538.         carPrecedente = (**(**descPtr).TEH->edit).lineStarts[titoloScelto];
  539.         TESetSelect(carPrecedente, (**(**descPtr).TEH->edit).lineStarts[titoloScelto + 1], 
  540.                     (**descPtr).TEH->edit);
  541.         TEActivate((**descPtr).TEH->edit);
  542.     }
  543.     HUnlock((Handle)descField);
  544.     RGBBackColor(&BackColor);
  545.     SetPort(port);
  546. }
  547.  
  548. /*** "gestClick(elOrdHdl, &theEvent)"
  549. *
  550. *    Gestisce i "mouse Event" nel campo "ElOrd".
  551. *    L' attivazione/disabilitazione del bottone ok
  552. *    è gestita da questa procedura.
  553. *
  554. *    Par INPUT:    'elOrdHdl' il puntatore alla struttura dati attuale,
  555. *                'theEvent' il puntatore al record dell'evento attuale.
  556. ************************************************************************/
  557. void CGestisciElOrd::gestClick(EventRecord *theEvent)
  558. {
  559.     TEHandle     TEH = (***descField).TEH->edit;
  560.     RGBColor     BackColor;
  561.     short         titoloScelto;            // contiene una copia del dato globale
  562.     short         titoloAlto = GetCtlValue(scrollTEH->scroll);
  563.     Boolean     cambioNumerazione = false;
  564.     GrafPtr        port;
  565.     short        lineHeight;
  566.     TEStyleRec    **theStyle;
  567.  
  568.     GetPort(&port);
  569.     SetPort((GrafPtr)theDialog);
  570.     GetBackColor(&BackColor);
  571.     RGBBackColor(&backGround);
  572.     GlobalToLocal(&theEvent->where);
  573.     if (PtInRect(theEvent->where, &(**scrollTEH->scroll).contrlRect))
  574.         scrollTEH->clickInTE(theEvent);
  575.     else
  576.     {
  577.         if(    hasNumberField && 
  578.             PtInRect(theEvent->where, &(**numerazione->edit).viewRect) )
  579.         {
  580.             cambioNumerazione = true;
  581.     
  582.             if (editLine != nil && canModify)
  583.             {
  584.                 sostituisciDato();
  585.                 return;
  586.             }
  587.         }
  588.         else
  589.         {
  590.             theStyle = GetStylHandle((***descField).TEH->edit);
  591.             lineHeight = (**(**theStyle).styleTab).stHeight;
  592.             titoloScelto = titoloAlto + (theEvent->where.v - 
  593.                             (**TEH).viewRect.top)/lineHeight;
  594.             if (titoloScelto != this->titoloScelto)
  595.             {
  596.                 if (editLine != nil && canModify)
  597.                 {
  598.                     sostituisciDato();
  599.                     return;
  600.                 }
  601.             }
  602.             
  603.     
  604.             if (titoloScelto > (**TEH).nLines)
  605.                 this->titoloScelto = noTitle;
  606.             else
  607.             {                // è stato selezionato "titoloScelto"
  608.                 if (titoloScelto == (**TEH).nLines && canModify == true && 
  609.                     !cambioNumerazione && (**TEH).nLines < maxNumData)
  610.                 {
  611.                     TEHandle TEH = (***descField).TEH->edit;
  612.                     long pos = (**TEH).lineStarts[titoloScelto];
  613.                     short *numVal;
  614.                     short *numMasc;
  615.                     short maxNum = (**TEH).nLines;
  616.                     char    ret = '\r';
  617.                     
  618.                     TESetSelect(pos, pos, TEH);
  619.                     TEInsert(&ret, 1, TEH);
  620.                     this->titoloScelto = titoloScelto;
  621.                     HLock((Handle)num);
  622.                     numVal = *num;
  623.                     numMasc = &numVal[maxNumData];
  624.                     numMasc[maxNum] = numVal[maxNum] = noValue;
  625.                     HUnlock((Handle)num);
  626.                     insertEl(titoloScelto);
  627.                 }
  628.                 if (canModify == true)
  629.                 {
  630.                     this->titoloScelto = titoloScelto;
  631.                     modifyData(theEvent);
  632.                 }
  633.                 else if (titoloScelto < (**TEH).nLines)
  634.                 {
  635.                     short             carPrecedente = 0;
  636.                     CdataDescRec    **descPtr = NULL;
  637.                     short i;
  638.         
  639.                     HLock((Handle)descField);
  640.                     descPtr = *descField;
  641.                     for (i = 0; i < nField; i++, descPtr++)
  642.                     {
  643.                         carPrecedente = (**(**descPtr).TEH->edit).lineStarts[titoloScelto];
  644.                         TESetSelect(carPrecedente, (**(**descPtr).TEH->edit).lineStarts[titoloScelto + 1], 
  645.                                     (**descPtr).TEH->edit);
  646.                         TEActivate((**descPtr).TEH->edit);
  647.                     }
  648.                     HUnlock((Handle)descField);
  649.                     this->titoloScelto = titoloScelto;
  650.                     if (cambioNumerazione)
  651.                         scambiaValori();
  652.                 }
  653.             }
  654.         }
  655.         RGBBackColor(&BackColor);
  656.         SetPort(port);
  657.     }
  658. }
  659.  
  660. /*** modifyData()
  661. *
  662. *    Permette la modifica dei dati
  663. *
  664. ****************************************************/
  665. void CGestisciElOrd::modifyData(EventRecord *theEvent)
  666. {
  667.     if (editLine == nil)
  668.     {
  669.         Rect        editRect;
  670.         TEHandle     TEH = (***descField).TEH->edit;
  671.         TEStyleRec    **theStyle;
  672.         short         lineHeight;
  673.         short         titoloAlto = GetCtlValue(scrollTEH->scroll);
  674.         long         inizio, fine, len;
  675.  
  676.         theStyle = GetStylHandle(TEH);
  677.         lineHeight = (**(**theStyle).styleTab).stHeight;
  678.         editLine = new CGestisci_TE;
  679.         if (editLine == NULL)
  680.             return;
  681.         editRect = (**TEH).viewRect;
  682.         editRect.top += (titoloScelto - titoloAlto) * lineHeight;
  683.         editRect.bottom = editRect.top + lineHeight;
  684.         editLine->IGestisci_TE(    theDialog, 0, editRect, &backGround, 
  685.                                 (**(***descField).TEH->edit).txFont, 
  686.                                 (**(***descField).TEH->edit).txSize,
  687.                                 false, false, true, true, 0, false, false, false);
  688.         editLine->canModify = true;
  689.         inizio = (**TEH).lineStarts[titoloScelto];
  690.         fine = (**TEH).lineStarts[titoloScelto + 1] - 1;
  691.         len = fine - inizio;
  692.         if (len > 0)
  693.         {
  694.             Cstring_P     strRead;
  695.             Ptr            pText;
  696.  
  697.             strRead.Istring_P();
  698.             HLock((**TEH).hText);
  699.             pText = *(**TEH).hText;
  700.             strRead.Copy(0, (short)len, &pText[inizio]);
  701.             editLine->sostituisciTesto(&strRead);
  702.             HUnlock((**TEH).hText);
  703.         }
  704.         editLine->attivaTesto(true, true);
  705.     }
  706.     else
  707.         editLine->clickInTE(theEvent);
  708. }
  709.  
  710. /*** sostituisciDato()
  711. *
  712. *    Permette la modifica dei dati
  713. *
  714. ****************************************************/
  715. Boolean CGestisciElOrd::sostituisciDato(void)
  716. {
  717.     Boolean cancellato = false;
  718.  
  719.     if (editLine != nil)
  720.     {
  721.         TEHandle TEH = (***descField).TEH->edit;
  722.         char pt = '.';
  723.         long inizio, fine, len;
  724.  
  725.         editLine->disattivaTesto();
  726.         TEDeactivate(TEH);
  727.         inizio = (**TEH).lineStarts[titoloScelto];
  728.         fine = (**TEH).lineStarts[titoloScelto + 1];
  729.         TESetSelect(inizio, fine, TEH);
  730.         TEDelete(TEH);
  731.         len = (**(*editLine).edit).teLength;
  732.         if (len > 1 || (len == 1 && (**(**(*editLine).edit).hText) != '.'))
  733.         {
  734.             char *strRead;
  735.  
  736.             HLock((**(*editLine).edit).hText);
  737.             strRead = *(**(*editLine).edit).hText;
  738.             TEInsert(strRead, len, TEH);
  739.             if (strRead[len - 1] != '.')
  740.             {
  741.                 TEInsert(&pt, 1L, TEH);
  742.                 len++;
  743.             }
  744.             pt = '\r';
  745.             TEInsert(&pt, 1L, TEH);
  746.             HUnlock((**(*editLine).edit).hText);
  747.         }
  748.         else
  749.         {
  750.             short *numVal;
  751.             short *numMasc;
  752.             short i, maxNum =  (**TEH).nLines;
  753.  
  754.             HLock((Handle)num);
  755.             numVal = *num;
  756.             numMasc = &numVal[maxNumData];
  757.             if (numVal[titoloScelto] != noValue)
  758.                 scambiaValori();
  759.             CancellaEl(titoloScelto);
  760.             for (i = titoloScelto; i < maxNum; i++)
  761.                 numVal[i] = numVal[i + 1];
  762.             for (i = 0; i < maxNum; i++)
  763.                 if (numMasc[i] >= titoloScelto)
  764.                     numMasc[i] -= 1;
  765.             titoloScelto = noTitle;
  766.             TEDeactivate(TEH);
  767.             HUnlock((Handle)num);
  768.             cancellato = true;
  769.         }
  770.         delete editLine;
  771.         editLine = nil;
  772.     }
  773.     return(cancellato);
  774. }
  775.  
  776. /*** inserisce un carattere
  777. *
  778. ************************************/
  779. void CGestisciElOrd::insertChar(EventRecord *theEvent)
  780. {
  781.     if (editLine != nil)
  782.         editLine->keyStroke(theEvent);
  783. }
  784.  
  785. /*** "listaFont(elOrdHdl)"
  786. *
  787. *    Gestisce l'updateEvt nei campi definiti come "user"
  788. *
  789. *    Par INPUT:    'elOrdHdl' il puntatore alla struttura dati attuale,
  790. *
  791. ************************************************************************/
  792. void CGestisciElOrd::updateEvt(void)
  793. {
  794.     CdataDescRec    **descPtr = NULL;
  795.     GrafPtr            port;
  796.     Rect            viewRect;
  797.     short            fontSize, fontNum;
  798.     short            i;
  799.  
  800.     GetPort(&port);
  801.     SetPort(theDialog);
  802.     fontSize = theDialog->txSize;
  803.     fontNum = theDialog->txFont;
  804.     HLock((Handle)descField);
  805.     descPtr = *descField;
  806.     for (i = 0; i < nField; i++, descPtr++)
  807.     {
  808.         TextFont((**descPtr).fontTitle);
  809.         TextSize((**descPtr).sizeTitle);
  810.         viewRect = (**(**descPtr).TEH->edit).viewRect;
  811.         MoveTo(viewRect.left + 4, viewRect.top - 2);
  812.         DrawString((**descPtr).title);
  813.         (**descPtr).TEH->updateEvt();
  814.     }
  815.     TextFont(fontNum);
  816.     TextSize(fontSize);
  817.     HUnlock((Handle)descField);
  818.     if (editLine != NULL)
  819.         editLine->updateEvt();
  820.     SetPort(port);
  821. }
  822.  
  823. /*** "disabilita(elOrdHdl, &theEvent)"
  824. *
  825. *    Gestisce i click nella scroll Bar.
  826. *
  827. *    Non tratta il doppio click e lo scroll mentre il bottone é abbassato.
  828. *
  829. *****************************************************************************/
  830. void CGestisciElOrd::disabilita()
  831. {
  832.     if (canModify && editLine != NULL)
  833.         sostituisciDato();
  834.     else
  835.     {
  836.         CdataDescRec    **descPtr = NULL;
  837.         short             i;
  838.  
  839.         HLock((Handle)descField);
  840.         descPtr = *descField;
  841.         for (i = 0; i < nField; i++, descPtr++)
  842.             (**descPtr).TEH->disattivaTesto();
  843.         HUnlock((Handle)descField);
  844.     }
  845. }
  846.  
  847. /*** "abilita(elOrdHdl, &theEvent)"
  848. *
  849. *    Gestisce i click nella scroll Bar.
  850. *
  851. *    Non tratta il doppio click e lo scroll mentre il bottone é abbassato.
  852. *
  853. *    Par INPUT:    'elOrdHdl' il puntatore alla struttura dati attuale,
  854. *                'theEvent' il puntatore al record dell'evento attuale;
  855. *****************************************************************************/
  856. void CGestisciElOrd::abilita(void)
  857. {
  858.     ;
  859. }
  860.  
  861. /*** "KeyDown(elOrdHdl, &theEvent)"
  862. *
  863. *    Gestisce i click nella scroll Bar.
  864. *
  865. *    Non tratta il doppio click e lo scroll mentre il bottone é abbassato.
  866. *
  867. *    Par INPUT:    'elOrdHdl' il puntatore alla struttura dati attuale,
  868. *                'theEvent' il puntatore al record dell'evento attuale;
  869. *****************************************************************************/
  870. Boolean CGestisciElOrd::KeyDown(EventRecord *theEvent)
  871. {
  872.     if (canModify && editLine != nil)
  873.     {
  874.         editLine->keyStroke(theEvent);
  875.         return(false);
  876.     }
  877.     else
  878.     {
  879.         register char    theChar = (char)((theEvent->message & keyCodeMask) >> 8);
  880.         const char        kUp = 0x7E;
  881.         const char        kDown = 0x7D;
  882.         short            scrollF = 0;
  883.  
  884.         switch(theChar)
  885.         {
  886.             case kUp:
  887.                 scrollF = +1;
  888.                 titoloScelto = (titoloScelto > 0) ? titoloScelto - 1 : 0;
  889.                 break;
  890.             case kDown:
  891.                 scrollF = -1;
  892.                 titoloScelto = (titoloScelto < (**(***descField).TEH->edit).nLines -1)    ? titoloScelto + 1
  893.                                                                                     : (**(***descField).TEH->edit).nLines - 1;
  894.                 break;
  895.         }
  896.         if (scrollF != 0)
  897.         {
  898.             CdataDescRec    **descPtr;
  899.             short            i;
  900.  
  901.             if (numerazione != NULL)
  902.                 numerazione->scrollFactor(scrollF);
  903.             HLock((Handle)descField);
  904.             descPtr = *descField;
  905.             for (i = 0; i < nField; i++, descPtr++)
  906.                 (**descPtr).TEH->scrollFactor(scrollF);
  907.             showActSel();
  908.             return(false);
  909.         }
  910.     }
  911.     return(true);
  912. }
  913.  
  914. /*** "TEidle(elOrdHdl)"
  915. *
  916. *    Gestisce i click nella scroll Bar.
  917. *
  918. *    Gestisce il blink sul campo selezionato.
  919. *
  920. *    Par INPUT:    'elOrdHdl' il puntatore alla struttura dati attuale,
  921. *****************************************************************************/
  922. void CGestisciElOrd::TEidle(void)
  923. {
  924.     if (editLine != NULL)
  925.         editLine->blink();
  926. }
  927.  
  928. /****
  929.  *    sostituisciTesto_gestDLOG(elOrdHdl, str, len);
  930.  *
  931.  *    inserisce del testo nel textEdit corrente
  932.  *
  933.  ***/
  934. Boolean CGestisciElOrd::sostituisciTesto(short nEl, Cstring_P *dataStr)
  935. {
  936.     if (nEl == nField)
  937.     {
  938.         CdataDescRec    **descPtr = NULL;
  939.         short             i;
  940.  
  941.         HLock((Handle)descField);
  942.         descPtr = *descField;
  943.         HLock((Handle)dataStr);
  944.         for (i = 0; i < nField; i++, descPtr++, dataStr++)
  945.             (**descPtr).TEH->sostituisciTesto(dataStr);
  946.         HUnlock((Handle)descField);
  947.         sostituisciNumerazione();
  948.         return(false);
  949.     }
  950.     else
  951.         return(true);
  952. }
  953.  
  954. /****
  955.  *    cancellaTesto(elOrdHdl);
  956.  *
  957.  *    inserisce del testo nel textEdit corrente
  958.  *
  959.  ***/
  960. Boolean CGestisciElOrd::cancellaTesto(void)
  961. {
  962.     CdataDescRec    **descPtr = NULL;
  963.     Cstring_P        str;
  964.     short            i;
  965.  
  966.     str.Istring_P();
  967.     HLock((Handle)descField);
  968.     descPtr = *descField;
  969.     for (i = 0; i < nField; i++, descPtr++)
  970.         (**descPtr).TEH->sostituisciTesto(&str);
  971.     HUnlock((Handle)descField);
  972.     sostituisciNumerazione();
  973.     return(false);
  974. }
  975.  
  976. /****
  977.  *    aggiungiTesto(elOrdHdl, str, len);
  978.  *
  979.  *    inserisce del testo nel textEdit corrente
  980.  *
  981.  ***/
  982. Boolean CGestisciElOrd::aggiungiTesto(short nEl, Cstring_P *dataStr)
  983. {
  984.     if (nEl == nField)
  985.     {
  986.         Cstring_P        str2;
  987.         CdataDescRec    **descPtr = NULL;
  988.         short             i;
  989.  
  990.         str2.Istring_P(2);
  991.         str2.Ctransfer("\r");
  992.         HLock((Handle)descField);
  993.         descPtr = *descField;
  994.         for (i = 0; i < nField; i++, descPtr++, dataStr++)
  995.         {
  996.             (**descPtr).TEH->aggiungiTesto(dataStr);
  997.             (**descPtr).TEH->aggiungiTesto(&str2);
  998.         }
  999.         HUnlock((Handle)descField);
  1000.         sostituisciNumerazione();
  1001.         return(false);
  1002.     }
  1003.     else
  1004.         return(true);
  1005. }
  1006.